﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin
{
    public partial class AdminHotPosition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckResponse("4013");
        }

        protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.VisibleIndex < 0)
            {
                return;
            }
            TL_HotPosition obj = this.ASPxGridView1.GetRow(e.VisibleIndex) as TL_HotPosition;
            TL_Company_Position p=DataContextFactory.Create().TL_Company_Position.Where(t=>t.PositionCode==obj.PositionCode).FirstOrDefault();
            if(p==null)
            {
                return;
            }
            if (e.DataColumn.Caption == "公司")
            {
                e.Cell.Text = p.TL_Company.CompanyName;
            }
            if (e.DataColumn.Caption == "职位")            {
                
                e.Cell.Text =p.PositionName;
            }
        }


        protected void abtnuploadblogo_Click(object sender, EventArgs e)
        {
            TL_HotPosition obj = this.ASPxGridView1.GetRow(this.ASPxGridView1.EditingRowVisibleIndex) as TL_HotPosition;
            FileUpload fu = (FileUpload)this.ASPxGridView1.FindEditRowCellTemplateControl((DevExpress.Web.ASPxGridView.GridViewDataColumn) this.ASPxGridView1.Columns["Image"],"FileUploadb");
            Image img = (Image)this.ASPxGridView1.FindEditRowCellTemplateControl((DevExpress.Web.ASPxGridView.GridViewDataColumn)this.ASPxGridView1.Columns["Image"], "imgb");
            string fullFileName = fu.PostedFile.FileName;
            string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
            string typeFileName = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
            if (typeFileName == "jpg" || typeFileName == "gif" || typeFileName == "png" || typeFileName == "bmp")
            {
                string fn = obj.PositionCode.ToString() + "." + typeFileName;
                string path = Server.MapPath(@"..\Images\HtmlInfo") + "\\" + fn;
                System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath(@"..\Images\HtmlInfo") + "\\" + obj.Image);
                if (fi.Exists)
                {
                    fi.Delete();
                }
                obj.Image = fn;
                var dc = DataContextFactory.Create();
                var n = dc.TL_HotPosition.Where(t => t.PositionCode == obj.PositionCode).FirstOrDefault();
                if (n != null)
                {
                    n.Image = fn;
                    dc.SubmitChanges();
                }
                fu.SaveAs(path);
                img.ImageUrl = path;
            }
            else
            {
                Response.Write("<script>alert('错误的图片文件格式');history.back();</script>");
            }
        }
    }
}